<?php
include '../class/class.detail.php';


session_start();

$id_user=$_SESSION['id_user'];
$id_det_usulan=$_REQUEST['id_det_usulan'];
$id_persetujuan=$_REQUEST['id_persetujuan'];
$alasan=$_REQUEST['alasan'];

$error = "";
$msg = false;
$ekstensi = array('pdf', 'jpg', 'jpeg');
$direktori = 'dokumen';
$size = 2000000; //2mb
$nama_file='';
if(($id_persetujuan=='1' || $id_persetujuan=='2') && $alasan==null){
	$error='Deskripsi / Alasan Penolakan Harus Diisi';
}elseif($id_persetujuan=='5' && empty($_FILES['file_surat']['tmp_name'])){
	$error='File Persetujuan Harap Diupload';
}else{
	if(!empty($_FILES['file_surat']['tmp_name']) || $_FILES['file_surat']['tmp_name'] != 'none'){
		$tmp = explode('.', $_FILES['file_surat']['name']);
	    $fileEks = $tmp[count($tmp) - 1];
	    $fileName = $tmp[0];
	    $nama_file = "proses_".$id_user."_".$id_det_usulan."_".$_FILES['file_surat']['name'];

	    if (in_array($fileEks, $ekstensi)) {
	        if ($_FILES['file_surat']['size'] <= $size) {

	            if (file_exists('../dokumen/' . $nama_file)) {
	                $error = "File Sudah Ada, Silahkan Hapus File Dahulu";
	            } else {
	                
	                $save = move_uploaded_file($_FILES['file_surat']['tmp_name'], '../' . $direktori . '/' . $nama_file);
	                if(!$save){
	                    $error="Gagal Simpan File";
	                }
	            }
	        } else {
	            $error = 'Maksimal Ukuran File 2mb';
	        }
	    } else {
	        $error = 'Format File hanya PDF, JPG dan JPEG';
	    }


	    @unlink($_FILES['file_surat']);
	}else{
		$nama_file='';
	}


	$detail=new Detail();

	$detail->id_detail=$id_det_usulan;
	$detail->id_tanggapan=$id_persetujuan;
	print_r($nama_file);
	$detail->nama_dokumen=$nama_file;
	$detail->keterangan=$alasan;
	$detailDao=new DetailDao();
	$hasil=$detailDao->prosesDetail($detail);
	if($hasil){
		$msg=true;
	}else{
		$error="Proses Gagal dilakukan";
	}


	
}

$data=array(
		'msg' => $msg,
		'error' => $error,
		);

echo json_encode($data);

?>